//
//  BaseTopView.m
//  markmap
//
//  Created by 甘世清 on 2024/3/29.
//

#import "BaseTopView.h"
#import "LookMapViewController.h"
@implementation BaseTopView
- (void)SQ_UIViewForSelfView{
    self.backgroundColor =  UIColor.clearColor;

    [self addSubview:self.headerLabel];
    [self.headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.mas_equalTo(54);
            make.top.left.equalTo(self).offset(12);
    }];
    [self addSubview:self.nameLabel];
    [self.nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(74);
        make.height.mas_equalTo(24);
        make.top.equalTo(self.headerLabel).offset(2);
    }];
    [self addSubview:self.timeLabel];
    [self.timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(74);
        make.top.equalTo(self.nameLabel.mas_bottom).offset(4);
    }];
    [self addSubview:self.messageLabel];
    [self.messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.top.equalTo(self.headerLabel.mas_bottom).offset(12);
        make.right.equalTo(self.mas_right).offset(-12);
    }];
    

    [self addSubview:self.mapView];
    [self.mapView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(12);
        make.right.equalTo(self.mas_right).offset(-12);
        make.top.equalTo(self.messageLabel.mas_bottom).offset(10);
        make.height.mas_equalTo(280);
    }];
    UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.backgroundColor = UIColor.clearColor;
    [btn addTarget:self action:@selector(SQ_mapViewBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.mapView);
    }];
    
    [self addSubview:self.mapImageView];
    [self.mapImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(16);
        make.bottom.equalTo(self.mapView.mas_bottom).offset(-4);
        make.right.equalTo(self.mas_right).offset(-16);
        make.height.mas_equalTo(60);
    }];
    
    [self addSubview:self.likeBtn];
    [self addSubview:self.collectBtn];
    [self.likeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-65);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.headerLabel);
    }];
    [self.collectBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.mas_right).offset(-5);
        make.width.mas_equalTo(60);
        make.height.mas_equalTo(44);
        make.centerY.equalTo(self.headerLabel);
    }];
    WS(weakSelf);
    [self.likeBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_isLikeButtonClick];
    }];
    [self.collectBtn setTouchBlock:^(SQCustomButton * _Nonnull button) {
        [weakSelf SQ_iscollectButtonClick];
    }];
    
    [self mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(SCREEN_WIDETH);
        make.bottom.equalTo(self.mapView.mas_bottom).offset(10);
    }];
}

- (void)setModel:(PointModel *)model{
    _model = model;
    self.mapImageView.model = self.model;
    [self.headerLabel.headerImage sd_setImageWithURL:SQURLImage(model.portrait)];
    self.nameLabel.text = model.name;
    self.timeLabel.text = [NSString SQ_getTheNumberDayTime:10];
    self.messageLabel.text = self.model.remark;
    if (model.isLike) {
        self.likeBtn.image = SQGetImage(@"yidianzan");
    } else {
        self.likeBtn.image = SQGetImage(@"arrow");
    }
    if (model.isCollect) {
        self.collectBtn.image = SQGetImage(@"yishoucang");
    } else {
        self.collectBtn.image = SQGetImage(@"shoucang");
    }
    [self SQ_mapView];
    [self layoutIfNeeded];
}
-(void)SQ_mapView{
    [self.mapView removeAnnotations:self.mapView.annotations];
    CLLocationCoordinate2D coordinate2D = CLLocationCoordinate2DMake(self.model.lat, self.model.lng);
       float zoomLevel = 0.005;
       MKCoordinateRegion region = MKCoordinateRegionMake(coordinate2D, MKCoordinateSpanMake(zoomLevel, zoomLevel));
    BaseAnntation *pointAnnotation = [[BaseAnntation alloc] init];
    pointAnnotation.coordinate =  coordinate2D;
    pointAnnotation.model = self.model;
    [self.mapView addAnnotation:pointAnnotation];
    [self.mapView showAnnotations:@[pointAnnotation] animated:YES];
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

-(void)SQ_isLikeButtonClick{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setValue:self.model.userid forKey:@"pid"];
    if (self.model.isLike) {
        [dic setValue:@"0" forKey:@"type"];
    } else {
        [dic setValue:@"1" forKey:@"type"];
    }
    [HttpTool post:Url13 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model.isLike = !self.model.isLike;
        if (self.model.isLike) {
            self.likeBtn.image = SQGetImage(@"yidianzan");
            self.model.likeNum ++;
        } else {
            self.likeBtn.image = SQGetImage(@"arrow");
            self.model.likeNum --;
        }
        if (self.isLikeBtnBlock) {
            self.isLikeBtnBlock(self.model);
        }
        [self SQ_mapView];
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_iscollectButtonClick{
    NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
    [dic setValue:self.model.userid forKey:@"pid"];
    if (self.model.isCollect) {
        [dic setValue:@"0" forKey:@"type"];
    } else {
        [dic setValue:@"1" forKey:@"type"];
    }
    [HttpTool post:Url14 paramDic:dic successBlock:^(BaseModel * _Nonnull httpModel) {
        self.model.isCollect = !self.model.isCollect;
        if (self.model.isCollect) {
            self.collectBtn.image = SQGetImage(@"yishoucang");
        } else {
            self.collectBtn.image = SQGetImage(@"shoucang");
        }
    } andFailBloc:^(BaseModel * _Nonnull httpModel, NSError * _Nullable error) {
        [YJProgressHUD showMsgWithoutView:httpModel.message];
    }];
}
-(void)SQ_mapViewBtnClick{
    LookMapViewController * vc = [[LookMapViewController alloc]init];
    vc.model = self.model;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}


- (void)dealloc {
    _mapView.showsUserLocation = NO;
    _mapView.userTrackingMode  = MKUserTrackingModeNone;
    [_mapView.layer removeAllAnimations];
    [_mapView removeAnnotations:_mapView.annotations];
    [_mapView removeOverlays:_mapView.overlays];
    [_mapView removeFromSuperview];
    _mapView.delegate = nil;
    _mapView = nil;
}
//设置大头针
- (MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation{
    
    if (![annotation isKindOfClass:[BaseAnntation class]]) {// 过滤掉MKUserLocation
        return nil;
    }
    BaseAnntation* ann = (BaseAnntation *)annotation;
    BaseAnnotationView *annotationView = (BaseAnnotationView*)[self.mapView dequeueReusableAnnotationViewWithIdentifier:ann.model.annotationViewIdentifier];
    if (!annotationView) {
       annotationView = [[BaseAnnotationView alloc] initWithAnnotation:ann
                                                     reuseIdentifier:ann.model.annotationViewIdentifier];
        annotationView.opaque = NO;
    }
    annotationView.model = ann.model;
    annotationView.bgView.hidden = YES;
    return annotationView;
}
 
 
-(void)SQ_PushPeople{
    PeopleDetailViewController * vc = [[PeopleDetailViewController alloc]init];
    vc.uid = self.model.uid;
    [[Manager SQ_getCurrentViewController].navigationController pushViewController:vc animated:YES];
}


/** 懒加载地图视图 */
- (MKMapView *)mapView{
    if (!_mapView) {
        _mapView = [[MKMapView alloc] init];
        /** 设置地图类型 */
        _mapView.mapType = MKMapTypeMutedStandard;
        _mapView.userTrackingMode = MKUserTrackingModeFollow;
        //设置地图可缩放
        _mapView.zoomEnabled = YES;
        _mapView.layer.cornerRadius = 12;
        _mapView.layer.masksToBounds = YES;
        //设置地图可滚动
        _mapView.scrollEnabled = YES;
        //设置地图可旋转
        _mapView.rotateEnabled = YES;
        //设置显示用户显示位置
        _mapView.showsUserLocation = YES;
        //为MKMapView设置delegate
        _mapView.delegate = self;
        _mapView.showsBuildings = YES;
        _mapView.showsTraffic = YES;
    }
    return _mapView;
}
- (UILabel *)nameLabel{
    if (!_nameLabel) {
        _nameLabel = [[UILabel alloc]init];
        _nameLabel.font = FONT_MEDIUM(16);
        _nameLabel.textColor = UIColor.whiteColor;
        _nameLabel.backgroundColor = UIColor.clearColor;
        _nameLabel.numberOfLines = 0;
      
    }
    return _nameLabel;
}

- (HeaderView *)headerLabel{
    if (!_headerLabel) {
        _headerLabel = [[HeaderView alloc]init];
        _headerLabel.headerImage.contentMode = UIViewContentModeScaleAspectFill;
        [_headerLabel.btn addTarget:self action:@selector(SQ_PushPeople) forControlEvents:UIControlEventTouchUpInside];
        _headerLabel.clipsToBounds = YES;
        _headerLabel.layer.cornerRadius = 27;
        _headerLabel.layer.masksToBounds = YES;
    }
    return _headerLabel;
}

- (UILabel *)timeLabel{
    if (!_timeLabel) {
        _timeLabel = [[UILabel alloc]init];
        _timeLabel.font = FONT_REGULAR(12);
        _timeLabel.textColor = SQColor999999;
        _timeLabel.backgroundColor = UIColor.clearColor;
    }
    return _timeLabel;
}

- (UILabel *)messageLabel{
    if (!_messageLabel) {
        _messageLabel = [[UILabel alloc]init];
        _messageLabel.font = FONT_REGULAR(14);
        _messageLabel.textColor = UIColor.whiteColor;
        _messageLabel.backgroundColor = UIColor.clearColor;
        _messageLabel.numberOfLines = 0;
    }
    return _messageLabel;
}
- (MapImageView *)mapImageView{
    if (!_mapImageView) {
        _mapImageView = [[MapImageView alloc]init];
    }
    return _mapImageView;
}
- (SQCustomButton *)likeBtn{
    if (!_likeBtn) {
        _likeBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(34, 34) midmargin:0];
        _likeBtn.image = SQGetImage(@"arrow");
        _likeBtn.backgroundColor = UIColor.clearColor;
    }
    return _likeBtn;
}
- (SQCustomButton *)collectBtn{
    if (!_collectBtn) {
        _collectBtn = [[SQCustomButton alloc]initType:SQCustomButtonRightImageType imageSize:CGSizeMake(34, 34) midmargin:0];
        _collectBtn.image = SQGetImage(@"shoucang");
        _collectBtn.backgroundColor = UIColor.clearColor;
    }
    return _collectBtn;
}




 
@end
